const { Sequelize } = require('sequelize');
const { dbLogger } = require('../../logger/index.js');

const sequelize = new Sequelize(process.env.DB_DATABASE, process.env.DB_USER, process.env.DB_PASSWORD, {
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    dialect: 'mysql',
    logging: msg => dbLogger.info(msg),
    timezone: '+08:00'
});

sequelize.authenticate().then(() => {
    console.log('数据库连接成功');
}).catch(err => {
    console.log('数据库连接失败：' + err);
});
sequelize.sync({ alter: true }).then(() => {
    console.log('手动同步数据库成功');
}).catch(error => {
    console.error('手动同步数据库失败', error);
});

module.exports = sequelize;